package elitech.vietnam.weatherfashion.config;

import java.lang.reflect.Type;
import java.util.ArrayList;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import elitech.vietnam.weatherfashion.dto.FriendDTO;
import elitech.vietnam.weatherfashion.dto.MemberDTO;
import elitech.vietnam.weatherfashion.dto.PostDTO;
import elitech.vietnam.weatherfashion.ultilities.ValueOption;
import elitech.vietnam.weatherfashion.weatherservice.WeatherObject;

public class GlobalValue {

	private final static String GLOBAL_VALUE_TAG = "GLOBAL_VALUE_TAG";
	private final static String LATITUDE_TAG = "LATITUDE_TAG";
	private final static String LONGTITUDE_TAG = "LONGTITUDE_TAG";

	private final static String TODAY_TAG = "TODAY_TAG";
	private final static String WEEKLY_TAG = "WEEKLY_TAG";
	private final static String STYLER_ALL_TAG = "STYLER_ALL_TAG";
	private final static String STYLER_FRIEND_TAG = "STYLER_FRIEND_TAG";
	private final static String MY_STYLE_TAG = "MY_STYLE_TAG";

	private final static String FRIEND_LIST_TUI_ADD_TAG = "FRIEND_LIST_TUI_ADD_TAG";
	private final static String FRIEND_LIST_ADD_TUI_TAG = "FRIEND_LIST_ADD_TUI_TAG";
	private final static String FRIEND_LIST_TAG = "FRIEND_LIST_TAG";
	private final static String USER_INFO_TAG = "USER_INFO_TAG";
	private final static String WEATHER_INFO_TAG = "WEATHER_INFO_TAG";
	private final static String OPTION_INFO_TAG = "OPTION_INFO_TAG";
	private final static String CITY_NAME_TAG = "OPTION_INFO_TAG";

	private static GlobalValue mInstance;

	public GlobalValue() {
	}

	private static SharedPreferences pref;

	public static synchronized GlobalValue getInstance(Context context) {
		if (mInstance == null)
			mInstance = new GlobalValue();
		pref = context.getSharedPreferences(GLOBAL_VALUE_TAG, 0);
		return mInstance;
	}

	private boolean write(String key, Object data) {
		try {
			Editor editor = pref.edit();
			Gson gson = new Gson();
			String jsonString = gson.toJson(data);
			editor.putString(key, jsonString);
			editor.commit();

			return true;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}

	private Object read(String key, Type type) {

		try {
			String jsonString = pref.getString(key, "");
			Gson gson = new Gson();
			Object rs = gson.fromJson(jsonString, type);
			return rs;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	public String readLatitude() {
		String rs = (String) read(LATITUDE_TAG, String.class);
		return rs == null ? "" : rs;
	}

	public void writeLatitude(String data) {
		write(LATITUDE_TAG, data);
	}

	public String readLongtitude() {
		String rs = (String) read(LONGTITUDE_TAG, String.class);
		return rs == null ? "" : rs;
	}

	public void writeLongtitude(String data) {
		write(LONGTITUDE_TAG, data);
	}

	public WeatherObject readWeatherInfo() {
		return (WeatherObject) read(WEATHER_INFO_TAG, WeatherObject.class);
	}

	public void writeWeatherInfo(WeatherObject data) {
		write(WEATHER_INFO_TAG, data);
	}

	public ValueOption readOptionInfo() {
		ValueOption option = (ValueOption) read(OPTION_INFO_TAG,
				ValueOption.class);
		return option == null ? new ValueOption() : option;
	}

	public void writeOptionInfo(ValueOption data) {
		write(OPTION_INFO_TAG, data);
	}

	public MemberDTO readUserInfo() {
		return (MemberDTO) read(USER_INFO_TAG, MemberDTO.class);
	}

	public void writeUserInfo(MemberDTO data) {
		write(USER_INFO_TAG, data);
	}

	@SuppressWarnings("unchecked")
	public ArrayList<FriendDTO> readFriendList() {
		Type type = new TypeToken<ArrayList<FriendDTO>>() {
		}.getType();
		return (ArrayList<FriendDTO>) read(FRIEND_LIST_TAG, type);
	}

	public void writeFriendList(ArrayList<FriendDTO> data) {
		write(FRIEND_LIST_TAG, data);
	}

	@SuppressWarnings("unchecked")
	public ArrayList<PostDTO> readToday() {
		Type type = new TypeToken<ArrayList<PostDTO>>() {
		}.getType();
		return (ArrayList<PostDTO>) read(TODAY_TAG, type);
	}

	public void writeToday(ArrayList<PostDTO> data) {
		write(TODAY_TAG, data);
	}

	@SuppressWarnings("unchecked")
	public ArrayList<ArrayList<PostDTO>> readWeekly() {
		Type type = new TypeToken<ArrayList<ArrayList<PostDTO>>>() {
		}.getType();
		return (ArrayList<ArrayList<PostDTO>>) read(WEEKLY_TAG, type);
	}

	public void writeWeekly(ArrayList<ArrayList<PostDTO>> data) {
		write(WEEKLY_TAG, data);
	}

	@SuppressWarnings("unchecked")
	public ArrayList<PostDTO> readStylerAll() {
		Type type = new TypeToken<ArrayList<PostDTO>>() {
		}.getType();
		return (ArrayList<PostDTO>) read(STYLER_ALL_TAG, type);
	}

	public void writeStylerAll(ArrayList<PostDTO> data) {
		write(STYLER_ALL_TAG, data);
	}

	@SuppressWarnings("unchecked")
	public ArrayList<PostDTO> readStylerFriend() {
		Type type = new TypeToken<ArrayList<PostDTO>>() {
		}.getType();
		return (ArrayList<PostDTO>) read(STYLER_FRIEND_TAG, type);
	}

	public void writeStylerFriend(ArrayList<PostDTO> data) {
		write(STYLER_FRIEND_TAG, data);
	}

	@SuppressWarnings("unchecked")
	public ArrayList<PostDTO> readMyStyle() {
		Type type = new TypeToken<ArrayList<PostDTO>>() {
		}.getType();
		return (ArrayList<PostDTO>) read(MY_STYLE_TAG, type);
	}

	public void writeMyStyle(ArrayList<PostDTO> data) {
		write(MY_STYLE_TAG, data);
	}

	@SuppressWarnings("unchecked")
	public ArrayList<FriendDTO> readFriendTuiAdd() {
		Type type = new TypeToken<ArrayList<FriendDTO>>() {
		}.getType();
		return (ArrayList<FriendDTO>) read(FRIEND_LIST_TUI_ADD_TAG, type);
	}

	public void writeFriendTuiAdd(ArrayList<FriendDTO> data) {
		write(FRIEND_LIST_TUI_ADD_TAG, data);
	}

	@SuppressWarnings("unchecked")
	public ArrayList<FriendDTO> readFriendAddTui() {
		Type type = new TypeToken<ArrayList<FriendDTO>>() {
		}.getType();
		return (ArrayList<FriendDTO>) read(FRIEND_LIST_ADD_TUI_TAG, type);
	}

	public void writeFriendAddTui(ArrayList<FriendDTO> data) {
		write(FRIEND_LIST_ADD_TUI_TAG, data);
	}

}
